/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.integral.web.controller.api;


import com.medusa.gruul.common.core.annotation.EscapeLogin;
import com.medusa.gruul.common.core.exception.ServiceException;
import com.medusa.gruul.common.core.util.CurUserUtil;
import com.medusa.gruul.common.core.util.Result;
import com.medusa.gruul.common.core.util.StringUtil;
import com.medusa.gruul.common.core.util.SystemCode;
import com.medusa.gruul.common.data.tenant.ShopContextHolder;
import com.medusa.gruul.common.dto.CurUserDto;
import com.medusa.gruul.integral.service.IIntegralClockService;
import com.medusa.gruul.integral.web.model.dto.manager.IntegralClockDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * <p>
 * 积分签到表 前端控制器
 * </p>
 *
 * @author xiaoq
 * @since 2019-11-19
 */
@RestController
@RequestMapping("singIn")
@Api(tags = "签到相关接口")
public class IntegralClockController {
	@Autowired
	private IIntegralClockService integralClockService;



	/**
	 * 获取用户签到信息 当天是否进行了签到
	 *
	 * @return 用户签到信息
	 */
	@ApiOperation(value = "获取用户签到信息")
	@GetMapping("getUserInfo")
	public Result<IntegralClockDto> userSingIn() {
		CurUserDto feignCurUser = CurUserUtil.getHttpCurUser();
		String userId = feignCurUser.getUserId();
		if (StringUtil.isEmpty(userId)) {
			throw new ServiceException("数据错误", SystemCode.DATA_NOT_EXIST.getCode());
		}
		IntegralClockDto integralClockDto = integralClockService.userSingInInfo(userId);
		return Result.ok(integralClockDto);
	}

	/**
	 * 用户进行签到 修改签到状态并生成积分明细记录
	 *
	 * @return
	 */
	@ApiOperation(value = "用户签到")
	@PostMapping("updateStatus")
	public Result updateRunningDays() {
		CurUserDto feignCurUser = CurUserUtil.getHttpCurUser();
		String userId = feignCurUser.getUserId();
		if (StringUtil.isEmpty(userId)) {
			throw new ServiceException("数据错误", SystemCode.DATA_NOT_EXIST.getCode());
		}
		Integer clockIntegral = integralClockService.userSingIn(userId);
		return Result.ok(clockIntegral);
	}


	/**
	 * 生成用户签到默认信息
	 *
	 * @return
	 */
	@ApiOperation(value = "生成用户默认签到信息")
	@PostMapping("default")
	@EscapeLogin
	public Result userSingInDefault() {
		CurUserDto feignCurUser = CurUserUtil.getHttpCurUser();
		String userId = feignCurUser.getUserId();
		if (StringUtil.isEmpty(userId)) {
			throw new ServiceException("数据错误", SystemCode.DATA_NOT_EXIST.getCode());
		}
		integralClockService.addSingInDefault();
		return Result.ok();
	}

	/**
	 * 展示总天数 以及每天对应的积分值
	 *
	 * @return 可签到的周期 以及 每日对应的签到积分
	 */
	@ApiOperation(value = "签到总天数")
	@GetMapping("get/singInTotalDays")
	public Result singInTotalDays() {
		String shopId = ShopContextHolder.getShopId();
		HashMap<Integer, String> singInIntegralValue = integralClockService.getSingInIntegralValue(shopId);
		return Result.ok(singInIntegralValue);
	}
}
